<?php

class CategoriesController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}
	// Event by Category
	public function actionView($url)
	{
		$model = Categories::model()->findByAttributes(array('url'=>$url));
		// Get list categories
		$categories = Categories::model()->findAll('status="A" AND parent_id=0 ORDER BY position DESC');
		foreach ($categories as $category) {
			$flag = 0;
			if (!empty($category->categories)) {
				foreach ($category->categories as $childCategory) {
					if ($childCategory->url === $url) { $flag = 1; }
					$items[] = array(
						'label'=>$childCategory->title,
						'url'=>array('/Events/categories/view', 'url'=>$childCategory->url),
						'linkOptions'=>array('title'=>'Xem sự kiện trong ' . $childCategory->title),
						'active'=>($childCategory->url === $url)
					);
				}
				$categoriesArr[] = array(
					'label'=>$category->title,
					'url'=>array('/Events/categories/view', 'url'=>$category->url),
					'linkOptions'=>array('title'=>'Xem sự kiện trong ' . $category->title),
					'items'=>$items,
					'active'=>($category->url === $url || $flag === 1)
					
				);
				unset($items);
			}
			else {
				$categoriesArr[] = array(
					'label'=>$category->title,
					'url'=>array('/Events/categories/view', 'url'=>$category->url),
					'linkOptions'=>array('title'=>'Xem sự kiện trong ' . $category->title),
				);
			}
		}

		if (!empty($model)) {
			$criteria = new CDbCriteria;
			$criteria->condition = 'status="A" AND enddate >= NOW()';
			$criteria->with = array('eventCategory'=>array(
				'joinType'=>'INNER JOIN',               
				'condition'=>'eventCategory.category_id = '.$model->id
			));
			$dataProvider=new CActiveDataProvider('Events',
				array(
					'criteria'=>$criteria,
					'sort'=>array(
						'defaultOrder'=>'startdate ASC, enddate DESC',
					),
					'pagination'=>array(
						'pageSize'=>10,
						'pageVar'=>'page',
					),
				)
			);
		}
		else {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		$topevents = Events::model()->findAll('status="A" limit 4');
		
		$this->render('view', array(
			'dataProvider'=>$dataProvider,
			'categories'=>$categoriesArr,
			'topevents'=>$topevents,
			'model'=>$model
		));
	}
	// Event in Day
	public function actionDayEvent($day = null, $month = null, $year = null)
	{
		if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])) {
			$day = $_POST['day'];
			$month = $_POST['month'];
			$year = $_POST['year'];
		}
		$today = $year . '-' . $month . '-' . $day . ' 00:00:00';
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND DATE(startdate) = DATE("' . $today . '") AND enddate >= DATE("' . $today . '")';
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'startdate ASC, enddate ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		
		if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])) {
			$this->redirect(array('/Events/categories/dayEvent', 'day'=>$day, 'month'=>$month, 'year'=>$year));
		}
		
		$this->render('dayEvent', array(
			'dataProvider'=>$dataProvider,
			'date'=>$day . '/' . $month . '/' . $year,
		));
	}
	
	//////////////////////////////////////
	// Event by ticket: F, G, P
	//////////////////////////////////////
	public function actionFreeEvent()
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND ticket="F" AND enddate >= NOW()';
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'startdate ASC, enddate ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('freeEvent', array(
			'dataProvider'=>$dataProvider,
		));
	}

	public function actionGiftEvent()
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND ticket="G" AND enddate >= NOW()';
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'startdate ASC, enddate ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('giftEvent', array(
			'dataProvider'=>$dataProvider,
		));
	}
	
	public function actionPaidEvent()
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND ticket="P" AND enddate >= NOW()';
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'startdate ASC, enddate ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('paidEvent', array(
			'dataProvider'=>$dataProvider,
		));
	}
	// Event by featured/hot
	public function actionHotEvent()
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND (featured="Y" OR hot="Y") AND enddate >= NOW()';
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'numviews DESC, startdate ASC, enddate ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('hotEvent', array(
			'dataProvider'=>$dataProvider,
		));
	}
	
	// Event by Location
	public function actionLocationEvent($location)
	{
		switch($location)
		{
			case 'ha-noi':
				$locationName = 'Hà Nội';
				break;
			case 'ho-chi-minh':
				$locationName = 'Hồ Chí Minh';
				break;
			case 'da-nang':
				$locationName = 'Đà Nẵng';
				break;
			case 'hue':
				$locationName = 'Huế';
				break;
			case 'nha-trang':
				$locationName = 'Nha Trang';
				break;
			default:
				$locationName = '';
				break;
		}
		
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND title LIKE "%' . $locationName . '%" AND enddate >= NOW()';
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'startdate ASC, enddate ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('locationEvent', array(
			'dataProvider'=>$dataProvider,
			'location'=>$locationName,
		));
	}
}